%% Estimate nu by MLE based on MDD

function nu_est = MLE_nu(hat_theta, rep_base_se2, tol)
% INPUT: baseline study data hat_theta and rep_base_se2, maximum tolerence for nonlinear solver
% OUTPUT: EB estimate for nu
    
    % Initialize the optimization
    % options = optimoptions('fsolve', 'Display', 'off', 'TolFun', tol, 'MaxIterations', max_iter);
    % Use fzero to find nu
    % nu_est = fsolve(@(nu) foc_new(nu, hat_theta, rep_base_se2), init_guess, options);
    % Force nu_est to be non-negative
    % nu_est = max(nu_est, 0);

    nu_bounds = [0, 1000];
    options = optimset('Display', 'off', 'TolX', tol, 'TolFun', tol);
    [nu_est, neg_max_ll] = fminbnd(@(nu) - calculate_log_likelihood(nu, hat_theta, rep_base_se2), nu_bounds(1), nu_bounds(2), options);
    
end